package com.we.flink.userlabelplatform.tag;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import com.alibaba.fastjson.JSONObject;
import com.we.flink.userlabelplatform.table.TableTempToFixedRiskTag;

public class TagTempToFixedRiskTag {
    public long uid;
    public int has_repaytask_increase_quota_tag;
    public int repaytask_increase_quota_tag_expired_time;
    public int repaytask_increase_quota_tag_output_time;
    public int update_time;

    public TagTempToFixedRiskTag() {}

    public TagTempToFixedRiskTag(
            long uid,
            int has_repaytask_increase_quota_tag,
            int repaytask_increase_quota_tag_expired_time,
            int repaytask_increase_quota_tag_output_time,
            int update_time) {
        this.uid = uid;
        this.has_repaytask_increase_quota_tag = has_repaytask_increase_quota_tag;
        this.repaytask_increase_quota_tag_expired_time = repaytask_increase_quota_tag_expired_time;
        this.repaytask_increase_quota_tag_output_time = repaytask_increase_quota_tag_output_time;
        this.update_time = update_time;
    }

    public static TagTempToFixedRiskTag init(long uid) {
        return new TagTempToFixedRiskTag(uid, 0, 0, 0, 0);
    }

    public static SingleOutputStreamOperator<String> getFlatMapStream(
            StreamTableEnvironment tableEnv, StreamExecutionEnvironment env) {

        Table table = tableEnv.sqlQuery(TableTempToFixedRiskTag.QUERYSQL);
        DataStream<Tuple2<Boolean, TableTempToFixedRiskTag>> dataStream =
                tableEnv.toRetractStream(table, TableTempToFixedRiskTag.class);

        SingleOutputStreamOperator<String> out =
                dataStream
                        .filter(
                                new FilterFunction<Tuple2<Boolean, TableTempToFixedRiskTag>>() {
                                    @Override
                                    public boolean filter(
                                            Tuple2<Boolean, TableTempToFixedRiskTag> values)
                                            throws Exception {
                                        return values.f0;
                                    }
                                })
                        .uid("TagTempToFixedRiskTag-filter")
                        .keyBy(
                                new KeySelector<Tuple2<Boolean, TableTempToFixedRiskTag>, Long>() {
                                    @Override
                                    public Long getKey(
                                            Tuple2<Boolean, TableTempToFixedRiskTag> values)
                                            throws Exception {
                                        return values.f1.getUid();
                                    }
                                })
                        .flatMap(
                                new RichFlatMapFunction<
                                        Tuple2<Boolean, TableTempToFixedRiskTag>, String>() {
                                    ValueState<TagTempToFixedRiskTag> tempToFixedRiskTagVState;

                                    @Override
                                    public void open(Configuration parameters) throws Exception {
                                        ValueStateDescriptor<TagTempToFixedRiskTag> valueStateDesc =
                                                new ValueStateDescriptor<>(
                                                        "tempToFixedRiskTagVState",
                                                        TagTempToFixedRiskTag.class);
                                        tempToFixedRiskTagVState =
                                                getRuntimeContext().getState(valueStateDesc);
                                    }

                                    @Override
                                    public void flatMap(
                                            Tuple2<Boolean, TableTempToFixedRiskTag> values,
                                            Collector<String> out)
                                            throws Exception {
                                        final int has_quota_tag = 1;
                                        long uid = values.f1.getUid();
                                        int update_time =
                                                (int) (values.f1.getUpdate_time().getTime() / 1000);
                                        int task_end_time =
                                                (int)
                                                        (values.f1.getTask_end_time().getTime()
                                                                / 1000);
                                        int output_time =
                                                (int) (values.f1.getOutput_time().getTime() / 1000);

                                        TagTempToFixedRiskTag tmpState =
                                                tempToFixedRiskTagVState.value();
                                        TagTempToFixedRiskTag curState =
                                                tmpState == null
                                                        ? TagTempToFixedRiskTag.init(uid)
                                                        : tmpState;

                                        if (update_time > curState.getUpdate_time()) {
                                            JSONObject jsonObject = new JSONObject();
                                            jsonObject.put("uid", uid);

                                            if (has_quota_tag
                                                    != curState
                                                            .getHas_repaytask_increase_quota_tag()) {
                                                jsonObject.put(
                                                        "has_repaytask_increase_quota_tag",
                                                        has_quota_tag);
                                                curState.has_repaytask_increase_quota_tag =
                                                        has_quota_tag;
                                            }
                                            if (task_end_time
                                                    != curState
                                                            .getRepaytask_increase_quota_tag_expired_time()) {
                                                jsonObject.put(
                                                        "repaytask_increase_quota_tag_expired_time",
                                                        task_end_time);
                                                curState.repaytask_increase_quota_tag_expired_time =
                                                        task_end_time;
                                            }
                                            if (output_time
                                                    != curState
                                                            .getRepaytask_increase_quota_tag_output_time()) {
                                                jsonObject.put(
                                                        "repaytask_increase_quota_tag_output_time",
                                                        output_time);
                                                curState.repaytask_increase_quota_tag_output_time =
                                                        output_time;
                                            }
                                            if (jsonObject.size() > 1) {
                                                out.collect(jsonObject.toString());
                                                curState.update_time = update_time;
                                                tempToFixedRiskTagVState.update(curState);
                                            }
                                        }
                                    }
                                });
        return out;
    }

    public long getUid() {
        return uid;
    }

    public int getHas_repaytask_increase_quota_tag() {
        return has_repaytask_increase_quota_tag;
    }

    public int getRepaytask_increase_quota_tag_expired_time() {
        return repaytask_increase_quota_tag_expired_time;
    }

    public int getRepaytask_increase_quota_tag_output_time() {
        return repaytask_increase_quota_tag_output_time;
    }

    public int getUpdate_time() {
        return update_time;
    }
}
